package com.itheima.service.impl;

import com.itheima.mapper.EmpMapper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.option.ClazzOption;
import com.itheima.pojo.option.JobOption;
import com.itheima.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author zzw
 * @date 2025/4/27
 * @description 报表统计实现类
 */
@Slf4j
@Service
public class ReportServiceImpl implements ReportService {


    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private StudentMapper studentMapper;

    /**
     * 获取员工岗位统计数据
     *
     * @return List<Map < String, Integer>> 员工岗位统计数据
     */
    @Override
    public JobOption getEmpJobData() {
        // 1. 调用mapper查询员工岗位统计数据
        List<Map<String, Object>> jobData = empMapper.countEmpJobData();
        log.info("员工岗位数据统计：{}", jobData);

        // 2.封装统计结果
        List<String> jobList = jobData.stream().map(data -> data.get("pos").toString()).toList();
        List<Long> dataList = jobData.stream().map(data -> (Long) data.get("total")).toList();


        // 3. 返回结果
        return new JobOption(jobList, dataList);
    }

    /**
     * 获取员工性别统计数据
     *
     * @return List<Map < String, Object>> 员工性别统计数据
     */
    @Override
    public List<Map<String, Object>> getEmpGenderData() {
        return empMapper.countEmpGenderData();
    }

    /**
     * 获取学生学历统计数据
     *
     * @return List<Map < String, Object>> 学生学历统计数据
     */
    @Override
    public List<Map<String, Object>> getStudentDegreeData() {
        return studentMapper.countStudentDegreeData();
    }

    /**
     * 获取学生班级统计数据
     *
     * @return ClazzOption 学生班级统计数据
     */
    @Override
    public ClazzOption getStudentCountData() {
        // 1. 调用mapper查询班级统计数据
        List<Map<String, Object>> clazzData = studentMapper.countStudentClazzData();
        log.info("学生班级统计数据：{}", clazzData);

        // 2. 封装统计结果
        List<String> clazzList = clazzData.stream().map(data -> data.get("name").toString()).toList();
        List<Long> dataList = clazzData.stream().map(data -> (Long) data.get("total")).toList();

        // 3. 返回结果
        return new ClazzOption(clazzList, dataList);
    }
}
